package com.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.core.framework.db.DBTools;
import com.core.framework.freemarker.engine.TplTemplate;
import com.core.framework.spring.appcontext.SpringAppContext;
import com.core.framework.web.easyweb.context.ServletActionContext;

/**
 * Rule 架构 应用帮助类
 * @author 刘涛
 * @since Jun 7, 2012
 */
public class AppUtils {
	public static void getDTO(String tableName)
	{
		DBTools.getDTO(tableName);
	}
	/**
	 * 在任意类中注入类  默认通过名称注入并检测内部的注入关系
	 * @param beanClass 要注入的类
	 * @return
	 */
	public static Object autowiredBean(Class<?> beanClass )
	{
		return SpringAppContext.getInstance().getAppContext().getAutowireCapableBeanFactory().autowire(beanClass, 1, true);
	}
	/**
	 * 在任意类中注入类
	 * @param beanClass 要注入的类
	 * @param dependencyCheck
	 * @return
	 */
	public Object autowiredBean(Class<?> beanClass , boolean dependencyCheck)
	{
		return SpringAppContext.getInstance().getAppContext().getAutowireCapableBeanFactory().autowire(beanClass, 1, dependencyCheck);
	}
	/**
	 * 在任意类中注入类
	 * @param beanClass 要注入的类
	 * @param autowireMode 类型：{ 	int AUTOWIRE_NO = 0;  	int AUTOWIRE_BY_NAME = 1;  	int AUTOWIRE_BY_TYPE = 2; 	int AUTOWIRE_CONSTRUCTOR = 3;}
	 * @param dependencyCheck
	 * @return
	 */
	public Object autowiredBean(Class<?> beanClass , int autowireMode, boolean dependencyCheck)
	{
		return SpringAppContext.getInstance().getAppContext().getAutowireCapableBeanFactory().autowire(beanClass, autowireMode, dependencyCheck);
	}
	/**
	 * 获取任意一个类的实例 
	 * @param <T>
	 * @param clazz  
	 * @return  T
	 */
	public static <T> T getBean(Class<T> clazz)
	{
		return SpringAppContext.getInstance().getAppContext().getBean(clazz);
	}
	/**
	 * <p> 获取一个类的实例，规则如下,鉴于Jdk1.5以后的注解方式,Spring 注解注入以后则系统会统一对注入的对象生成一个KEY</p>
	 * <p> 生成KEY的规则是:注入类的名称，并且首字母小写 。 举例{注入类：TestService  生成Key:testService} </p>
	 * @param <T>
	 * @param beanName  beanName    [KEY]
	 * @param clazz      对象类
	 * @return  T
	 */
	public static <T> T getBean(String beanName ,Class<T> clazz)
	{
		return SpringAppContext.getInstance().getAppContext().getBean(beanName,clazz);
	}
	/**
	 * 通过beanName 获取类对象
	 * @param beanName
	 * @return Object
	 */
	public static Object getBean(String beanName )
	{
		return SpringAppContext.getInstance().getAppContext().getBean(beanName);
	}
	/**
	 * 获取  request 上下文
	 * @return
	 */
	public static HttpServletRequest getRequest()
	{
			return ServletActionContext.getRequest();
	}
	/**
	 * 获取 response 上下文
	 * @return
	 */
	public static HttpServletResponse getResponse()
	{
		return ServletActionContext.getResponse();
	}
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
		TplTemplate templet = new TplTemplate();
		String tplPath = "E:/Java/workspace/Rule/WebRoot/test/" ;
		String templetName = "Test.htm" ;
		templet.init(tplPath, "E:/cache");
		Map<String,Object> rootMap = new HashMap<String,Object>();
		rootMap.put("abc", "----------------");
		System.out.print(templet.process(rootMap, templetName));
	}
}
